﻿using HLStock.Common;
using HLStock.MyExcep;
using Microsoft.AspNetCore.Authorization;

namespace HLStock.WmsApi.AuthenticationInjection
{
    public enum AuthorizeLevelEnum
    {
        RootOnly,
        ACL,
        AllUser
    }
    public enum ModPermissionEnum
    {
        Read, Write, Remove, Approved
    }
    public class MyAuthorizeAttribute : AuthorizeAttribute
    {
        public AuthorizeLevelEnum AuthorizeLevel { get; private set; }
        public ModPermissionEnum ModPermission { get; private set; }
        public string Module { get; private set; }
        public MyAuthorizeAttribute(AuthorizeLevelEnum authorizeLevel = AuthorizeLevelEnum.AllUser) : base(Const.BASE_RULE)
        {
            AuthorizeLevel = authorizeLevel;
            if (AuthorizeLevel == AuthorizeLevelEnum.ACL)
            {
                throw new MyEmptyOrNullDataException("当授权级别是“模块”时，必须提供模块信息");
            }
        }
        public MyAuthorizeAttribute(AuthorizeLevelEnum authorizeLevel, string module, ModPermissionEnum modPermission) : base(Const.BASE_RULE)
        {
            AuthorizeLevel = authorizeLevel;
            Module = module;
            ModPermission = modPermission;
            if (AuthorizeLevel == AuthorizeLevelEnum.ACL && String.IsNullOrEmpty(module))
            {
                throw new MyEmptyOrNullDataException("当授权级别是“模块”时，必须提供模块信息");
            }
            if (AuthorizeLevel != AuthorizeLevelEnum.ACL && !String.IsNullOrEmpty(module))
            {
                throw new MyException("当授权级别不是“模块”时，不能提供模块信息");
            }
        }
    }
}
